/*
 * UserManager.java
 *
 * Created on June 20, 2007, 2:41 AM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package jhub.management;

import java.sql.SQLException;
import java.util.LinkedList;
import jhub.Hub;
import jhub.UserSession;
import jhub.conn.exception.BadPasswdException;
import jhub.conn.exception.LoginErrorException;
import jhub.dao.DAOFactory;
import jhub.management.exception.UserNotFoundException;
import jhub.users.Admin;
import jhub.users.Registered;
import jhub.users.User;

/**
 *
 * @author dbotelho
 */
public class UserManager {
    public static enum UserType{user,registered,vip,operator,admin};
    
    public boolean ipBanned(String ip){
        return false;
    }
    
    public boolean validate(UserSession userSession) throws ClassNotFoundException, SQLException{
        //if()
        //DAOFactory.getBanListDAO().isIPBanned(userSession.getIp());
        return DAOFactory.getBanListDAO().isIPBanned(userSession.getIp());
    }
    
    public User getUser(String nick) throws ClassNotFoundException, SQLException{
        User user = new User();
        user.setHub(this.hub);
        user.setNick(nick);
        try {
            user = DAOFactory.getUserDAO().findByExample(user);
        } catch (UserNotFoundException ex) {
            ex.printStackTrace();
        }
        return user;
    }
        
    public Registered validate(String nick,String passwd) throws LoginErrorException, UserNotFoundException{
        Registered reg;
        reg = new Registered();
        
        reg.setNick(nick);
        reg.setHub(this.hub);
        
        try {
            reg = (Registered) DAOFactory.getUserDAO().findByExample(reg);
        } catch (ClassNotFoundException ex) {
            ex.printStackTrace();
            throw new UserNotFoundException();
        } catch (SQLException ex) {
            ex.printStackTrace();
            throw new UserNotFoundException();
        }
        
        if(reg.getPasswd().equals(passwd))
            return reg;
        else
            throw new BadPasswdException();
    }

    public boolean contains(String nick) {
        try {
            User user = new User();
            user.setHub(this.hub);
            user.setNick(nick);
            return DAOFactory.getUserDAO().contains(user);
        } catch (SQLException ex) {
            ex.printStackTrace();
        } catch (ClassNotFoundException ex) {
            ex.printStackTrace();
        }
        return false;
    }
    
    public void kick(Ban ban) throws ClassNotFoundException, SQLException{
        DAOFactory.getBanListDAO().save(ban);
    }

    public void create(User newUser) throws ClassNotFoundException, SQLException {
        DAOFactory.getUserDAO().save(newUser);
    }
 
    private Hub hub;
    /** Creates a new instance of UserManager */
    public UserManager(Hub hub) {
        this.hub = hub;
    }
    
}
